<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="真实姓名" prop="customerRealname">
      <el-input v-model="dataForm.customerRealname" placeholder="真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="customerNickname">
      <el-input v-model="dataForm.customerNickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="customerPhone">
      <el-input v-model="dataForm.customerPhone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="登陆密码" prop="customerPassword">
      <el-input v-model="dataForm.customerPassword" placeholder="登陆密码"></el-input>
    </el-form-item>
    <el-form-item label="微信登录open_id" prop="customerOpenid">
      <el-input v-model="dataForm.customerOpenid" placeholder="微信登录open_id"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="customerPic">
      <el-input v-model="dataForm.customerPic" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="会员等级" prop="customerLevel">
      <el-input v-model="dataForm.customerLevel" placeholder="会员等级"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          customerId: 0,
          customerRealname: '',
          customerNickname: '',
          customerPhone: '',
          customerPassword: '',
          customerOpenid: '',
          customerPic: '',
          customerLevel: '',
          addTime: '',
          updateTime: '',
          deleted: ''
        },
        dataRule: {
          customerRealname: [
            { required: true, message: '真实姓名不能为空', trigger: 'blur' }
          ],
          customerNickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          customerPhone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          customerPassword: [
            { required: true, message: '登陆密码不能为空', trigger: 'blur' }
          ],
          customerOpenid: [
            { required: true, message: '微信登录open_id不能为空', trigger: 'blur' }
          ],
          customerPic: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          customerLevel: [
            { required: true, message: '会员等级不能为空', trigger: 'blur' }
          ],
          addTime: [
            { required: true, message: '添加时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.customerId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.customerId) {
			this.$axios.get(`/customer/info/${this.dataForm.customerId}`).then((data)=>{
				if (data && data.data.code === 0) {
					this.dataForm.customerRealname = data.data.customer.customerRealname
					this.dataForm.customerNickname = data.data.customer.customerNickname
					this.dataForm.customerPhone = data.data.customer.customerPhone
					this.dataForm.customerPassword = data.data.customer.customerPassword
					this.dataForm.customerOpenid = data.data.customer.customerOpenid
					this.dataForm.customerPic = data.data.customer.customerPic
					this.dataForm.customerLevel = data.data.customer.customerLevel
				}
			}).catch((error)=>{
				console.log(error)
			})
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
			var params = {
				'customerId': this.dataForm.customerId || undefined,
				'customerRealname': this.dataForm.customerRealname,
				'customerNickname': this.dataForm.customerNickname,
				'customerPhone': this.dataForm.customerPhone,
				'customerPassword': this.dataForm.customerPassword,
				'customerOpenid': this.dataForm.customerOpenid,
				'customerPic': this.dataForm.customerPic,
				'customerLevel': this.dataForm.customerLevel
			}
 			this.$axios.post(`/customer/${!this.dataForm.customerId ? 'save' : 'update'}`, params).then((data)=>{
				if (data && data.data.code === 0) {
				  this.$message({
				    message: '操作成功',
				    type: 'success',
				    duration: 1500,
				    onClose: () => {
				      this.visible = false
				      this.$emit('refreshDataList')
				    }
				  })
				} else {
				  this.$message.error(data.data.msg)
				}
			}).catch((error)=>{
				console.log(error)
			})
          }
        })
      }
    }
  }
</script>
